package cn.edu.nuc.weiboclient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.nuc.wangchao.modle.FollowsInfo;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RelationshipItemAdapter extends BaseAdapter{

	private Context context = null;
	private List<FollowsInfo> followsInfos = null;
	private Map<Integer,View> viewMaps = new HashMap<Integer,View>();
	private AsyncImageLoader loader = new AsyncImageLoader();
	public RelationshipItemAdapter(Context context,List<FollowsInfo> followsInfos){
		this.context = context;
		this.followsInfos = followsInfos;
	} 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = viewMaps.get(position);
		if(rowView == null){
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			rowView = inflater.inflate(R.layout.followitem, null);
			FollowsInfo followsInfo = (FollowsInfo)getItem(position);
			TextView nickTextView = (TextView)rowView.findViewById(R.id.followNickId);
			TextView nameTextView = (TextView)rowView.findViewById(R.id.followNameId);
			final ImageView headImageView = (ImageView)rowView.findViewById(R.id.followIconId);
			
			nickTextView.setText(followsInfo.getNick());
			nameTextView.setText(followsInfo.getName());
			
	
		
			if(!followsInfo.getHead().equals("")){
				Drawable headDrawable = loader.loadDrawable(followsInfo.getHead() + "/50", new AsyncImageLoader.ImageCallback() {
				
					@Override
					public void imageLoaded(Drawable imageDrawable) {
						headImageView.setImageDrawable(imageDrawable);
					}
				});
				if(headDrawable != null){
					headImageView.setImageDrawable(headDrawable);
				}
			}
			else{
				System.out.println(headImageView);
				headImageView.setImageResource(R.drawable.icon);
			}
			viewMaps.put(position, rowView);
		}
		return rowView;
	}
	@Override
	public int getCount() {
		return followsInfos.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return followsInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


}
